#include "mypushbutton.h"
#include <QDebug>
#include <QPropertyAnimation>

//MyPushButton::MyPushButton(QWidget *parent):QPushButton{parent}
//{

//};
MyPushButton::MyPushButton(QString normalImg,QString pressImg)
{
    this->normalImgPath=normalImg;
    this->pressImgPath=pressImg;
    QPixmap pix;
    int ret = pix.load(this->normalImgPath);
    if(!ret)
    {
        qDebug()<<"图片加载失败";
        return;
    }
    //设置图片固定大小
    this->setFixedSize(pix.size());
    //设置不规则图片样式
    this->setStyleSheet("QPushButton{border:none}");
    //设置图标
    this->setIcon(pix);
    //设置图标大小
    this->setIconSize(pix.size());
}
void MyPushButton::zoom1()
{
    //创建动画对象
    QPropertyAnimation * animation1 = new QPropertyAnimation(this,"geometry");
    //设置间隔时间，单位毫秒
    animation1->setDuration(200);
    //创建起始位置
    animation1->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //创建结束位置,向下走10
    animation1->setEndValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //设置缓和曲线（缓动动画）QEasingCurve::OutBounce为弹跳效果
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    //开始执行动画
    animation1->start();
}

void MyPushButton::zoom2()
{
    //创建动画对象
    QPropertyAnimation * animation1 = new QPropertyAnimation(this,"geometry");
    //设置间隔时间，单位毫秒
    animation1->setDuration(400);
    //创建起始位置
    animation1->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //创建结束位置,向下走10
    animation1->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置缓和曲线（缓动动画）QEasingCurve::OutBounce为弹跳效果
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    //开始执行动画
    animation1->start();
}
void MyPushButton::mousePressEvent(QMouseEvent * e)
{
    if(this->pressImgPath!="")
    {
        QPixmap pix;
        int ret = pix.load(this->pressImgPath);
        if(!ret)
        {
            qDebug()<<"图片加载失败";
            return;
        }
        //设置图片固定大小
        this->setFixedSize(pix.size());
        //设置不规则图片样式
        this->setStyleSheet("QPushButton{border:none}");
        //设置图标
        this->setIcon(pix);
        //设置图标大小
        this->setIconSize(pix.size());
    }
    //让父类执行其他内容
    return QPushButton::mousePressEvent(e);
}
void MyPushButton::mouseReleaseEvent(QMouseEvent * e)
{
    if(this->normalImgPath!="")
    {
        QPixmap pix;
        int ret = pix.load(this->normalImgPath);
        if(!ret)
        {
            qDebug()<<"图片加载失败";
            return;
        }
        //设置图片固定大小
        this->setFixedSize(pix.size());
        //设置不规则图片样式
        this->setStyleSheet("QPushButton{border:none}");
        //设置图标
        this->setIcon(pix);
        //设置图标大小
        this->setIconSize(pix.size());
    }
    return QPushButton::mouseReleaseEvent(e);
}
